package com.frank.subject.application.controller;

import com.frank.common.entity.Result;
import com.frank.subject.application.convert.SubjectLabelDtoConverter;
import com.frank.subject.application.dto.SubjectLabelDto;
import com.frank.subject.domain.entity.SubjectLabelBo;
import com.frank.subject.domain.service.SubjectLabelDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Climb Zou
 * @version v0.0.1
 * @date 2024/1/23 14:17
 */
@Slf4j
@RestController
@RequestMapping("/subject/label")
public class SubjectLabelController {

    private final SubjectLabelDomainService subjectLabelDomainService;

    public SubjectLabelController(SubjectLabelDomainService subjectLabelDomainService) {
        this.subjectLabelDomainService = subjectLabelDomainService;
    }

    @PostMapping("/add")
    public Result<Boolean> addLabel(@RequestBody SubjectLabelDto subjectLabelDTO) {
        SubjectLabelBo subjectLabelBO = SubjectLabelDtoConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("adding subject label, label dto: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "Label name cannot be empty");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "Category id cannot be empty");
            Preconditions.checkNotNull(subjectLabelDTO.getSortNum(), "Label sort num cannot be empty");
            subjectLabelDomainService.insert(subjectLabelBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("Adding subject label error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDto subjectLabelDTO) {
        SubjectLabelBo subjectLabelBO = SubjectLabelDtoConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("update subject label, label dto: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "Label id cannot be empty");
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "Label name cannot be empty");
            Boolean result = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("Adding subject label error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDto subjectLabelDTO) {
        SubjectLabelBo subjectLabelBO = SubjectLabelDtoConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("delete subject label, label dto: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "Label id cannot be empty");
            Boolean result = subjectLabelDomainService.deleteByPrimary(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("Adding subject label error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @GetMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDto>> queryLabelByCategoryId(SubjectLabelDto subjectLabelDTO) {
        SubjectLabelBo subjectLabelBO = SubjectLabelDtoConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("query label by category id, label dto: {}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "Category id cannot be empty");
            List<SubjectLabelBo> subjectLabelBos = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDto> subjectLabelDtos = SubjectLabelDtoConverter.INSTANCE.convertBOListToLabelDTOList(subjectLabelBos);
            return Result.ok(subjectLabelDtos);
        } catch (Exception e) {
            log.error("Adding subject label error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }
}
